package testng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @author cheng.huaxing
 * @date 2019-03-12
 */
@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowProcessControllerTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeClass
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    //@Test
    public void testStartProcess() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .get("/process/start").characterEncoding("UTF-8")
                .contentType("application/json")
                .param("processDefinitionKey", "deletetest111"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    @Test
    public void testStartProcess2() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/start").characterEncoding("UTF-8")
                .contentType("application/json")
                .content("{\n" +
                        "  \"processDefinitionKey\":\"LIYANG_TEST_ABC\",\n" +
                        "  \"variables\":{\n" +
                        "    \n" +
                        "  }\n" +
                        "}"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testGenProcessDiagram() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .get("/process/diagram/447501").characterEncoding("UTF-8")
                .contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
    }

    //@Test
    public void testTodoTasks() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/todo").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"appId\":10,\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":10\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testProcessed() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/processed").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":6,\n" +
                        "  \"timeStart\":\"\",\n" +
                        "  \"timeEnd\":\"\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testApply() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/apply").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":20,\n" +
                        "  \"timeStart\":\"\",\n" +
                        "  \"timeEnd\":\"\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testStart() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/apply").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processDefinitionKey\":\"ceshihuiqiangongneng001\",\n" +
                        "  \"variables\":{\n" +
                        "    \n" +
                        "  }\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testList() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/list").characterEncoding("UTF-8")
                .content("{\n" +
                        "    \"searchName\":\"\",\n" +
                        "    \"timeStart\":\"\",\n" +
                        "    \"timeEnd\":\"\",\n" +
                        "    \"applyUserName\":\"\",\n" +
                        "    \"processStatus\":\"executing\",\n" +
                        "    \"appId\":\"10\",\n" +
                        "    \"pageNum\":1,\n" +
                        "    \"pageSize\":6,\n" +
                        "    \"timeOut\":false\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testProcessInstanceDiagramAndInfo() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/diagramAndInfo").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"437508\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testProcessInstanceCurrentTasks() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/currentTasks").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"437508\",\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":10\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testProcessInstanceHandleLogs() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/handleLogs").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"437508\",\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":10\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testProcessInstanceMutiTaskInfo() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/mutiTaskInfo").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processInstanceId\":\"660001\",\n" +
                        "  \"taskId\":\"660006\",\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":10\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testProcessInstanceHandle() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/handle").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processStatus\":\"1\",\n" +
                        "  \"processInstanceId\":\"660001\",\n" +
                        "  \"comment\":\"挂起\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);

        result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/handle").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processStatus\":\"2\",\n" +
                        "  \"processInstanceId\":\"660001\",\n" +
                        "  \"comment\":\"激活\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        content = result.getResponse().getContentAsString();
        jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);

        result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/handle").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processStatus\":\"3\",\n" +
                        "  \"processInstanceId\":\"660001\",\n" +
                        "  \"comment\":\"撤单\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        content = result.getResponse().getContentAsString();
        jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);

        result = mockMvc.perform(MockMvcRequestBuilders
                .post("/process/handle").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"processStatus\":\"4\",\n" +
                        "  \"processInstanceId\":\"660001\",\n" +
                        "  \"comment\":\"终止\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        content = result.getResponse().getContentAsString();
        jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
}